package du.zhaofeng2019;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;


public class SplashActivity extends AppCompatActivity {

    static int Get_Data_Finished = 1;
    static int Get_permission_call = 2;
    static int Get_permission_internet = 3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_splash);


        // TODO: 2019/1/7 动态权限：电话，上网
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (ContextCompat.checkSelfPermission(SplashActivity.this, "Manifest.permission.CALL_PHONE") != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(SplashActivity.this, new String[]{android.Manifest.permission.CALL_PHONE}, Get_permission_call);
            }
            if (ContextCompat.checkSelfPermission(SplashActivity.this, "Manifest.permission.INTERNET") != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(SplashActivity.this, new String[]{android.Manifest.permission.INTERNET}, Get_permission_internet);
            }
        }


        ImageView imageView = findViewById(R.id.logopic);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        if (new SqlMethod(SplashActivity.this).getCount() == 0) {
                            //如果数据库为空，则读 网络 或 xml
                            new DataStore(SplashActivity.this, myHandler);
                        } else {
                            //数据库有，直接发消息，跳列表页
                            myHandler.sendEmptyMessage(Get_Data_Finished);
                        }
                    }
                }).start();

            }
        });


    }


    Handler myHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == Get_Data_Finished) {
                Intent intent = new Intent(SplashActivity.this, ListViewActivity.class);
                startActivity(intent);
                SplashActivity.this.finish();
            }
        }
    };


}
